/*
 * @Name: 
 * @Author: LiuCaiLin
 * @Date: 2020-04-22 16:22:13
 * @LastEditors: LiuCaiLin
 * @LastEditTime: 2020-04-26 17:48:48
 */

import {
  request
} from '../../utils/request.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: '',
    orderList: '',
    page: 1,
    flag: true,
    pagesize: 10,
    baseUrl: '',
    loadingInfo: '下拉加载更多数据...',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success(res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
    that.setData({
      baseUrl: app.globalData.url
    })
    that.orderList()
  },

  orderList() {
    let that = this
    wx.request({
      url: 'https://www.lythds.com/orderList',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token')
      },
      method: 'POST',
      data: {
        ids: [2, 9],
        pagesize: that.data.pagesize,
        page: that.data.page,

      },

      success: res => {
        if (res.data.error_code == 20000) {
          this.setData({
            loadingInfo: '暂无数据...'
          })
        } else {
          let orderList = res.data.data
          let page = ++that.data.page
          if (orderList.length < that.data.pagesize) {
            var flags = false
            this.setData({
              loadingInfo: '暂无更多数据...'
            })
          } else {
            var flags = true
            this.setData({
              loadingInfo: '下拉加载更多数据...'
            })
          }

          orderList.forEach(item => {
            if (item.status == '9') {
              item.showBtn = false
            } else {
              item.showBtn = true
            }
          })

          let newOrderList = []
          if (that.data.orderList.length > 0) {
            newOrderList = that.data.orderList.concat(orderList)
          } else {
            newOrderList = orderList
          }

          that.setData({
            orderList: newOrderList,
            page,
            flag: flags
          })
        }
      }

    })
  },

  /** 
   * 退款
  */
  tuikuan(e) {
    let that = this

    console.log('数组长度', that.data.orderLength)


    wx.showModal({
      title: '提示', //提示的标题,
      content: '确定要退款吗？', //提示的内容,
      confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
      confirmColor: '#3CC51F', //确定按钮的文字颜色,
      success: res => {
        if (res.confirm) {
          request({
            url: `orderStatus`,
            token: wx.getStorageSync('token'),
            method: 'POST',
            data: {
              id: e.currentTarget.dataset.bean.id,
              status: 9
            }
          }).then(res => {
            wx.navigateTo({ url: '/pages/payTwo/payTwo' });
            console.log('点击退款返回的操作', res)
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });


  },

  /** 
   * 上拉加载更多
  */
  loadMore() {
    console.log('加载更多')
    if (this.data.flag) {
      this.orderList()

    }


  }

})